package com.mikepenz.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.bean.CardBean;
import com.mikepenz.bean.UserBean;
import com.mikepenz.interactor.LoginInteractor;
import com.mikepenz.interactor.imp.LoginInteracorImp;
import com.mikepenz.presenter.LoginPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.utils.SystemUtils;
import com.mikepenz.view.LoginView;
import com.quickdv.until.LogHelper;

/**
 * Created by Administrator on 2015/9/14.
 */
public class LoginPresenterImp implements RequestListener<UserBean>,LoginPresenter {

    Context c;
    LoginView view;
    LoginInteractor interactor ;
    LogHelper log;
    String username,pwd;

    public LoginPresenterImp(Context c){
        this(c, ((LoginView) c));
    }

    public LoginPresenterImp(Context c,LoginView view){
        this.c= c;
        this.view = view;
        interactor = new LoginInteracorImp(c,this);
        log = LogHelper.getInstance();
    }

    @Override
    public void Success(String tag, UserBean result) {
        view.dimissLoading();
        if (view.remenber()){
            SharedPreferences sharedPreferences = c.getSharedPreferences("login",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("username",username);
            editor.putString("pwd",pwd);
            editor.commit();
        }
        CookicUntil.setUser(result);
        if (result.getCards()!=null && result.getCards().size()>0) {
            CardBean bean = result.getCards().get(0);
            bean.setIsCheck(true);
            CookicUntil.setCardBean(bean);
        }
        ((Activity) c).finish();
    }

    @Override
    public void Error(String tag, String error) {

        view.dimissLoading();
        view.userNameOrPwdError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        log.v(tag,error + "" );
        view.dimissLoading();
        view.showAlert(c.getResources().getString(R.string.system_error));
    }

    @Override
    public boolean userNameCheck(String username) {
        if(TextUtils.isEmpty(username)) {
            view.userNameFailed("用户名不能为空");
            return false;
        }
        else if(!SystemUtils.isPhone(username)){
            view.userNameFailed("用户名必须是手机号");
            return  false;
        }
        else
            return true;
    }

    @Override
    public boolean pwdCheck(String pwd) {
        if(TextUtils.isEmpty(pwd)) {
            view.pwdFailed("用户密码不能为空");
            return false;
        }else
            return true;
    }

    @Override
    public void login(String userName, String pwd) {
        view.showLoading();
        this.username = userName;
        this.pwd = pwd;
        interactor.login(userName,pwd,null);
    }
}
